package com.yh.hljapp.modules.adatper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yh.hljapp.R;
import com.yh.hljapp.gddb.gbmanager.BackMatManager;
import com.yh.hljapp.gddb.gbmanager.MatCountManager;
import com.yh.hljapp.gddb.gbmanager.MatHeadManager2;
import com.yh.hljapp.gddb.gdbean.MatHead;
import com.yh.hljapp.modules.recycleviewutil.onMoveAndSwipedListener;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：BackMatListAdapter
 * <p>
 * 创建时间：2017.06.04.10:07
 * *****************************
 */
public class BackMatListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements onMoveAndSwipedListener {

    private Context ctx;
    private List<MatHead> datas;
    private String type;

    public BackMatListAdapter(Context ctx,List<MatHead> datas,String type) {
        this.ctx = ctx;
        this.datas = datas;
        this.type=type;
    }

    public void setDatas(List<MatHead> datas){
        this.datas=datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(ctx).inflate(R.layout.item_mat_wl, parent,
                false);
        return new MatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MatHead head = datas.get(position);
        ((MatViewHolder)holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onItemClick(head);
                }
            }
        });
        ((MatViewHolder)holder).tvFhck.setText(head.getFhckName());
        ((MatViewHolder)holder).tvGhdw.setText(head.getGhdwName());
        ((MatViewHolder)holder).tvXsfs.setText(head.getXsfsName());
        ((MatViewHolder)holder).tvYwy.setText(head.getYwyName());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    private OnItemClickListener listener;

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        return false;
    }

    @Override
    public void onItemDismiss(int position) {

        Long id = datas.get(position).getId();
        if ("2".equals(type)){
            MatHeadManager2.getInstance(ctx).delAllById(id);
            MatCountManager.getInstance(ctx).delAllMatByHeadId(id);//提交成功后删除缓存
        }else if ("3".equals(type)){
            MatHeadManager2.getInstance(ctx).delAllById(id);
            BackMatManager.getInstance(ctx).delAllByHeadId(id);//提交成功后删除缓存
        }
        //删除mItems数据
        datas.remove(position);
        //删除RecyclerView列表对应item
        notifyItemRemoved(position);
    }

    public interface OnItemClickListener{
        void onItemClick(MatHead head);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener=listener;
    }

    static class MatViewHolder extends RecyclerView.ViewHolder{
        @InjectView(R.id.tv_jlbh)
        TextView tvJlbh;
        @InjectView(R.id.tv_ghdw)
        TextView tvGhdw;
        @InjectView(R.id.tv_xsfs)
        TextView tvXsfs;
        @InjectView(R.id.tv_ywy)
        TextView tvYwy;
        @InjectView(R.id.tv_fhck)
        TextView tvFhck;

        MatViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
